/* $Header: sig_mem.c,v 2.2 01/10/02 10:33:05 przybyls Exp $ */

/***********************************************************************/
/*                                                                     */
/*                  Copyright Hewlett-Packard Co. 1991                 */
/*                                                                     */
/***********************************************************************/

/***********************************************************************/
/* MODULE NAME:  sig_mem.c                                             */
/* ============                                                        */
/*                                                                     */
/* Author:  Lesley Freed                                               */
/*                                                                     */
/* MODULE PURPOSE:  To define the plot memory location Get/Set/Check   */
/*                  functions.                                         */
/*                                                                     */
/***********************************************************************/


/*****************************/
/*   GLOBAL INCLUDE FILES    */
/*****************************/

#include <typedefs.h>
#include <p_wkfile.h>
#include <wkfile.h>
#include <ramps.h>
#include <zone_config.h>
#include <i_op.h>
#include <zone_status.h>
#include <status.h>
#include <error.h>
#include <sig_types.h>
#include <sig_ui.h>
#include <iomap.h>
#include <dig_path.h>

#include <exception.h>
#include <glp.h>

/*****************************/
/*   LOCAL INCLUDE FILES     */
/*****************************/


/*****************************/
/*   GLOBAL VARIABLES        */
/*****************************/


/*****************************/
/*   EXTERNAL VARIABLES      */
/*****************************/

extern const PLOT_CONSTANTS NO_CHANGE_CONSTS;

/*****************************/
/*   LOCAL DEFINES           */
/*****************************/


/*****************************/
/*   STATIC VARIABLES        */
/*****************************/



/*****************************/
/*   IMPLEMENT SECTION       */
/*****************************/

/***********************************************************************/
/* FUNCTION:   CheckSignalMemLoc                                       */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:  To check if a memory address location is readable.  If    */
/*           it is then it can be assigned as a signal.                */
/*                                                                     */
/* RETURNED VALUE:  UI_ERR                                             */
/*                                                                     */
/* GLOBALS USED:  none                                                 */
/*                                                                     */
/* CALLS:  none                                                        */
/*                                                                     */
/* LIMITATIONS:  This function is intimately tied to the hardware, any */
/*               changes to the i/o map may require an update to the   */
/*               ranges used here.                                     */
/*                                                                     */
/***********************************************************************/


UI_ERR CheckSignalMemLoc( U_INT32 *pmem, DATA_SIZE size )
{

/* Memory map used:

   Address                Readable            Writable
   -------                --------            --------
   000000h                   X                   X
   100000h
   200000h
   300000h                   X                   X ( thru 30ffffh )
   400000h                   X
   500000h
   600000h
   700000h
   800000h
   900000h
   a00000h
   b00000h
   c00000h                   X                   X ( thru cffbffh )
   d00000h
   e00000h
   f00000h                   X ( > fff000h )

*/

   if ( ( (U_INT32)pmem >= 0x100000 ) && ( (U_INT32)pmem < 0x300000 ) )
   {
      return( INVALID_PARAM );
   }

   if ( ( (U_INT32)pmem >= 0x500000 ) && ( (U_INT32)pmem < 0xc00000 ) )
   {
      return( INVALID_PARAM );
   }

   if ( ( (U_INT32)pmem >= 0xd00000 ) && ( (U_INT32)pmem < 0xfff000 ) )
   {
      return( INVALID_PARAM );
   }

   if ( (U_INT32)pmem > 0xffffff )
   {
      return( INVALID_PARAM );
   }

   switch( size )
   {
      case SIZE_32_BIT:
      case SIZE_16_BIT:
      case SIZE_8_BIT:
      case SIZE_8_BIT_SIGNED:
      case SIZE_16_BIT_SIGNED:
         break;
      default:
         return( INVALID_PARAM );
   }

   if ( ( size == SIZE_8_BIT ) || ( size == SIZE_8_BIT_SIGNED ) )
   {
      return( OK );
   }

   if ( (U_INT32)pmem%2 == 0 )
   {
      return( OK );
   }

   return( INVALID_PARAM );
}

INT32 GetSignalMemLocAddr( pSIG_TABLE psignal )
{
   return( (INT32)psignal->plot );
}



/***********************************************************************/
/* FUNCTION:  SetSignalMemLoc                                          */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:  To set the signal type to a memory location.              */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:  Active_Wkfile, Signal1 or Signal2                    */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/

DATA_SIZE GetSignalMemLocSize( pSIG_TABLE psignal )
{
   return( psignal->plot_size );
}

UI_ERR SetSignalMemLoc ( pSIG_TABLE psignal, DATA_SIZE size, U_INT32 *pmem )
{
   UI_ERR check_setpt;

   check_setpt = CheckSignalMemLoc( pmem, size );

   if ( check_setpt != OK )
   {
      return( check_setpt );
   }

   INTERRUPTS_OFF
   psignal->active_wkfile->signal       = (BIT8)MEMORY_LOCATION;
   psignal->active_wkfile->plot         = pmem;
   psignal->active_wkfile->size         = (BIT8)size;

   psignal->signal                = MEMORY_LOCATION;
   psignal->plot                  = pmem;
   psignal->plot_size             = size;
   psignal->pconstants            = &NO_CHANGE_CONSTS;
   psignal->pscale                = GetSignalScaleFactor(MEMORY_LOCATION);
   psignal->ccomp_mode            = DISABLE_CCOMP;
   INTERRUPTS_ON

/* REVISIT:  is this complete? */
   if ( psignal->chan == SIGNAL1 )
   {
      LogSetptChange( SIGNAL1_TYPE, ((INT32)MEMORY_LOCATION), 0);
   }
   else if ( psignal->chan == SIGNAL2 )
   {
      LogSetptChange( SIGNAL2_TYPE, ((INT32)MEMORY_LOCATION), 0);
   }
/* End of REVISIT */

   return( OK );
}


/**************************************/
/* Diagnostic user interface routines */
/**************************************/

/***********************************************************************/
/* FUNCTION:  CheckWritableMemLoc                                      */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:  To see if the memory location, addr, is a writable        */
/*           location.                                                 */
/*                                                                     */
/* RETURNED VALUE:  UI_ERR, ui_err                                     */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/

UI_ERR CheckWritableMemLoc( U_INT32 *pmem, DATA_SIZE size )
{
   /* See if it is in the discrete IO section.  NOTE:  We will accept  */
   /* both readable and writable addresses.  In the case of a read     */
   /* only address a write operation will have no effect.              */
   /* All of the addresses are 8-bit so we will require that it is     */
   /* 8-bit data size.                                                 */
printf("call CheckWritableMemLoc\n");
return INVALID_PARAM;
   if ( ( ( (U_INT32)pmem >= IO_MIN ) && ( (U_INT32)pmem <= IO_MAX ) )  &&
        ( (  size == SIZE_8_BIT ) || ( size == SIZE_8_BIT_SIGNED ) ) )
   {
      return( OK );
   }

   /* Now we will allow you to write out to more than one byte at a time */
   if ( ( ( (U_INT32)pmem >= IO_MIN ) && ( (U_INT32)pmem <= IO_MAX ) )  &&
        ( (U_INT32)pmem%2 == 0 ) )
   {
      return( OK );
   }

   /* See if it is in SRAM or DRAM */
   if ( ( ( (U_INT32)pmem >= RAM ) && ( (U_INT32)pmem <= RAM_END ) ) ||
        ( ( (U_INT32)pmem >= DRAM_MIN ) && ( (U_INT32)pmem <= DRAM_MAX ) ) )
   {
      switch( size )
      {
         case SIZE_8_BIT:
         case SIZE_16_BIT:
         case SIZE_32_BIT:
         case SIZE_8_BIT_SIGNED:
         case SIZE_16_BIT_SIGNED:
            break;
         default:
            return( INVALID_PARAM );
      }

      /* We have accepted the address, so now lets see if it is   */
      /* compatible with the data size.                           */

      /* An 8-bit value does not have to be on an even word boun- */
      /* dary.                                                    */
      if ( ( size == SIZE_8_BIT ) || ( size == SIZE_8_BIT_SIGNED ) )
      {
         return( OK );
      }

      /* It is not 8-bit size, so is it on an even word boundary? */
      if ( (U_INT32)pmem%2 == 0 )
      {
         return( OK );
      }
   }
   return( INVALID_PARAM );
}

/***********************************************************************/
/* FUNCTION: GetDataSize                                               */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: To convert the data size from a number of bits to an       */
/*          enumerated type: DATA_SIZE                                 */
/*                                                                     */
/* RETURNED VALUE:  DATA_SIZE                                          */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/

UI_ERR GetDataSize( INT32 value, DATA_SIZE *size )
{
   UI_ERR error;

   error = OK;

   switch( value )
   {
      case 8:   *size = SIZE_8_BIT;
                break;

      case 16:  *size = SIZE_16_BIT;
                break;

      case 32:  *size = SIZE_32_BIT;
                break;

      case -8:  *size = SIZE_8_BIT_SIGNED;
                break;

      case -16: *size = SIZE_16_BIT_SIGNED;
                break;

      default:  *size = NOT_PLOT_SIGNAL;
                error = INVALID_PARAM;
   }
   return error;
}
